package com.bell.atensettings.settingDetails;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import org.apache.http.conn.util.InetAddressUtils;

import com.bell.atensettings.R;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class NetWorkInfoSettings extends Activity
{
    public String run(String[] cmdList, String exeHome) throws IOException
    {
        String result = "";
        try {
            ProcessBuilder process = new ProcessBuilder(cmdList);
            InputStream inputStream = null;
            byte[] buff;
            if (exeHome != null) {
                process.directory(new File(exeHome));
                process.redirectErrorStream(true);
                inputStream = process.start().getInputStream();
                buff = new byte[1024];
                while (inputStream.read(buff) != -1) {
                    result = result + new String(buff);
                }

            }
            if (inputStream != null)
                inputStream.close();

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }

    /**
     * 获取有线MAC地址
     * 
     * @return有线MAC地址
     */
    public String fetch_mac_eth()
    {
        String eth0_add = "未开启";

        try {
            File wlanFile = new File("/sys/class/net/eth0/address");
            if (wlanFile.exists()) {
                String[] cmdList = { "/system/bin/cat", "/sys/class/net/eth0/address" };
                eth0_add = run(cmdList, "system/bin/");
                eth0_add = eth0_add.substring(0, eth0_add.indexOf("\n"));

                if (eth0_add.length() > 27) {
                    String str = eth0_add.substring(0, 27);
                    eth0_add = str;
                }
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
        return eth0_add;
    }

    /**
     * 获取无线网卡MAC 地址
     * 
     * @return 无线MAC地址
     */
    public String fetch_mac_wlan()
    {
        String wlan_mac = "未开启";
        try {
            File wlanFile = new File("/sys/class/net/wlan0/address");
            if (wlanFile.exists()) {
                String[] cmdList = { "/system/bin/cat", "/sys/class/net/wlan0/address" };
                wlan_mac = run(cmdList, "system/bin/");
                wlan_mac = wlan_mac.substring(0, wlan_mac.indexOf("\n"));

                if (wlan_mac.length() > 28) {
                    String str = wlan_mac.substring(0, 28);
                    wlan_mac = str;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return wlan_mac;
    }

    /**
     * 获取本地IP地址
     * 
     * @return IP地址
     */
    private String getLocalIpAddress()
    {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())
                            && !inetAddress.getHostAddress().toString().equals("null")
                            && inetAddress.getHostAddress() != null) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("WifiPreference IpAddress", ex.toString());
        }
        return null;
    }

    @Override
    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.network_info);

        ((TextView) findViewById(R.id.network_ip)).setText(getLocalIpAddress());
        ((TextView) findViewById(R.id.network_wifimac)).setText(fetch_mac_wlan());
        ((TextView) findViewById(R.id.network_ethermac)).setText(fetch_mac_eth());
    }

    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }
}
